package com.fyzk.dialog;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tyjc.lottery.R;
import com.fyzk.utils.LotteryIDUtils;

import java.util.ArrayList;

import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.SAFEAMOUNT_ALL;
import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.SAFEAMOUNT_HAS;
import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.SAFEAMOUNT_NO;
import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.STATE_ALL;
import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.STATE_HASFILL;
import static com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity.STATE_NOFILL;

/**
 * Created by miao on 2017/6/8.
 */

public class TogetherFilterPop extends PopupWindow implements View.OnClickListener {

    private final LotteryFilterAdapter percentageAdapter;
    private String lotteryType_JZ = LotteryIDUtils.JZSPF_LOTTERY_ID_INT + "," + LotteryIDUtils
            .JZRFSPF_LOTTERY_ID_INT + "," + LotteryIDUtils.JZBF_LOTTERY_ID_INT + "," +
            LotteryIDUtils.JZZJQ_LOTTERY_ID_INT + "," + LotteryIDUtils.JZBQC_LOTTERY_ID_INT + ","
            + LotteryIDUtils.JZHH_LOTTERY_ID_INT;
    private String lotteryType_JL = LotteryIDUtils.JLDXF_LOTTERY_ID_INT + "," + LotteryIDUtils
            .JLHH_LOTTERY_ID_INT + "," +
            LotteryIDUtils.JLRSF_LOTTERY_ID_INT + "," + LotteryIDUtils.JLSF_LOTTERY_ID_INT + ","
            + LotteryIDUtils.JLSFC_LOTTERY_ID_INT;
    private String lotteryType_ZC = LotteryIDUtils.ZCBQC_LOTTERY_ID_INT + "," + LotteryIDUtils
            .ZCJQC_LOTTERY_ID_INT + "," +
            LotteryIDUtils.ZCSFC_LOTTERY_ID_INT + "," + LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT;

    private String lotteryType_BD = LotteryIDUtils.DCSPF_LOTTERY_ID_INT + "," + LotteryIDUtils
            .DCBF_LOTTERY_ID_INT + "," + LotteryIDUtils.DCZJQ_LOTTERY_ID_INT + "," +
            LotteryIDUtils.DCBQC_LOTTERY_ID_INT + "," + LotteryIDUtils.DCSXDS_LOTTERY_ID_INT;

    class LotteryFilterTage {

        LotteryFilterTage(String lotteryName, String filterTage) {
            this.filterTage = filterTage;
            this.lotteryName = lotteryName;
        }

        String filterTage;
        String lotteryName;
    }

    private GridView lotteryGrid;
    private RadioGroup progressRgp;
    private RadioGroup garanteeRgp;
    private Button btn_cancel;
    private Button btn_ok;

    private Context context;
    ArrayList<LotteryFilterTage> filterTages = new ArrayList<>();
    SparseBooleanArray mCheckItems;

    private int isSafeAmount = SAFEAMOUNT_ALL;
    private int state = STATE_NOFILL;

    public TogetherFilterPop(Context context, int width, int height) {
        super(width, height);
        this.context = context;
        View view = View.inflate(context, R.layout.layout_togetherbuy_filter, null);
        lotteryGrid = (GridView) view.findViewById(R.id.gridview_filter_lottery);
        progressRgp = (RadioGroup) view.findViewById(R.id.rdg_filter_progress);
        garanteeRgp = (RadioGroup) view.findViewById(R.id.rdg_filter_garantee);
        btn_cancel = (Button) view.findViewById(R.id.btn_filter_cancel);
        btn_ok = (Button) view.findViewById(R.id.btn_filter_ok);
        btn_cancel.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
        this.setContentView(view);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent));
        // 设置此参数获得焦点，否则无法点击
        this.setFocusable(true);
        filterTages.add(new LotteryFilterTage("全部", "0"));
        filterTages.add(new LotteryFilterTage("双色球", LotteryIDUtils.SSQ_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("竞彩足球", lotteryType_JZ));
        filterTages.add(new LotteryFilterTage("竞彩篮球", lotteryType_JL));
        filterTages.add(new LotteryFilterTage("北京单场", lotteryType_BD));
        filterTages.add(new LotteryFilterTage("福彩3D", LotteryIDUtils.FC3D_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("七乐彩", LotteryIDUtils.QLC_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("大乐透", LotteryIDUtils.DLT_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("排列三", LotteryIDUtils.PL3_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("排列五", LotteryIDUtils.PL5_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("七星彩", LotteryIDUtils.QXC_LOTTERY_ID_INT + ""));
        filterTages.add(new LotteryFilterTage("传统足彩", lotteryType_ZC ));
        lotteryGrid.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        percentageAdapter = new LotteryFilterAdapter(filterTages);
        mCheckItems = lotteryGrid.getCheckedItemPositions();
        lotteryGrid.setAdapter(percentageAdapter);
        progressRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_filter_progress_all:
                        state = STATE_ALL;
                        break;
                    case R.id.rb_filter_progress_fill:
                        state = STATE_HASFILL;
                        break;
                    case R.id.rb_filter_progress_unfill:
                        state = STATE_NOFILL;
                        break;
                }
            }
        });
        garanteeRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_filter_garantee_all:
                        isSafeAmount = SAFEAMOUNT_ALL;
                        break;
                    case R.id.rb_filter_garantee:
                        isSafeAmount = SAFEAMOUNT_HAS;
                        break;
                    case R.id.rb_filter_garantee_no:
                        isSafeAmount = SAFEAMOUNT_NO;
                        break;
                }
            }
        });
    }


    private class LotteryFilterAdapter extends BaseAdapter {

        private ArrayList<LotteryFilterTage> percentages;

        public LotteryFilterAdapter(ArrayList<LotteryFilterTage> percentages) {
            this.percentages = percentages;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return this.percentages.size();
        }

        @Override
        public Object getItem(int position) {
            return this.percentages.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(context, R.layout.item_filter_lottery, null);
            LotteryFilterTage item = filterTages.get(position);
            ((RadioButton) convertView).setOnCheckedChangeListener(null);
            ((RadioButton) convertView).setChecked(mCheckItems.get(position));
            final int index = position;
            ((RadioButton) convertView).setOnCheckedChangeListener(new CompoundButton
                    .OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    lotteryGrid.setItemChecked(index, isChecked);
                    percentageAdapter.notifyDataSetChanged();
                }
            });
            if (lotteryGrid.getCheckedItemPosition() == -1 && position == 0) {
                ((RadioButton) convertView).setChecked(true);
            }
            ((RadioButton) convertView).setText(item.lotteryName);
            return convertView;
        }
    }

    OnFilterOKListener listener;

    public void setOnFilterOKListener(OnFilterOKListener listener) {
        this.listener = listener;
    }

    public interface OnFilterOKListener {
        void onFilterOK(String filterTage, int garantee, int isfill);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_filter_cancel:
                this.dismiss();
                break;
            case R.id.btn_filter_ok:
                this.dismiss();
                if (listener != null) {
                    String filterTag = filterTages.get(lotteryGrid.getCheckedItemPosition()).filterTage;
                    listener.onFilterOK(filterTag,isSafeAmount,state);
                }
                break;
        }
    }
}
